home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / POSITION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-07  |  5.6 KB  |  250 lines

  1.  
  2. /* Position reporting-related user commands (derived from RIP)
  3.  *   (c) 1993 Brian A. Lantz
  4.  */
  5. #include "global.h"
  6. #ifdef GPS
  7. #include "commands.h"
  8. #include "mbuf.h"
  9. #include "netuser.h"
  10. #include "internet.h"
  11. #include "iface.h"
  12. #include "udp.h"
  13. #include "gps.h"
  14. #include "files.h"
  15.  
  16. #if !defined(_lint)
  17. static char rcsid[] OPTIONAL = "$Id: position.c,v 1.17 1997/09/07 21:18:28 root Exp root $";
  18. #endif
  19.  
  20. static uint32 PosHost = 0xffffffffU;
  21. char *CurrentPos;
  22. static struct udp_cb *GPS_cb;
  23. static struct timer POStimer;
  24.  
  25. static int docurrent (int argc,char *argv[],void *p);
  26. static int doposhostname (int argc,char *argv[],void *p);
  27. static int dolist (int argc,char *argv[],void *p);
  28. static int dotimer (int argc,char *argv[],void *p);
  29. static void POStick (void *v);
  30. static void gps_rx (struct iface *iface,struct udp_cb *sock,int16 cnt);
  31. static int gps_tx (void);
  32. static int doPOSkick (int argc,char *argv[],void *p);
  33.  
  34.  
  35. /* Start Position agent listening at local GPS UDP port */
  36. int
  37. POSstart(argc, argv, p)
  38. int argc OPTIONAL;
  39. char *argv[] OPTIONAL;
  40. void *p OPTIONAL;
  41. {
  42. struct socket lsock;
  43.  
  44.     lsock.address = INADDR_ANY;
  45.     lsock.port = IPPORT_GPS;
  46.  
  47.     if(GPS_cb == NULLUDP)
  48.         GPS_cb = open_udp(&lsock,gps_rx);
  49.  
  50.     return 0;
  51. }
  52.  
  53. int
  54. POSstop(argc,argv,p)
  55. int argc OPTIONAL;
  56. char *argv[] OPTIONAL;
  57. void *p OPTIONAL;
  58. {
  59.     (void) del_udp(GPS_cb);
  60.     GPS_cb = NULLUDP;
  61.     return 0;
  62. }
  63.  
  64.  
  65. static struct cmds Positioncmds[] = {
  66.     { "current",    docurrent,    0,    0,    NULLCHAR },
  67.     { "hostname",    doposhostname,    0,    0,    NULLCHAR },
  68.     { "kick",    doPOSkick,    0,    0,    NULLCHAR },
  69.     { "list",    dolist,        0,    0,    NULLCHAR },
  70.     { "timer",    dotimer,    0,    0,    NULLCHAR },
  71.     { NULLCHAR,    NULL,        0,    0,    NULLCHAR }
  72. };
  73.  
  74. int
  75. doposition(argc,argv,p)
  76. int argc;
  77. char *argv[];
  78. void *p;
  79. {
  80.     return subcmd(Positioncmds,argc,argv,p);
  81. }
  82.  
  83. /* Set the host that we are to report position data to */
  84. static int
  85. doposhostname(argc,argv,p)
  86. int argc;
  87. char *argv[];
  88. void *p OPTIONAL;
  89. {
  90.     if (argc > 1)
  91.         PosHost = resolve(argv[1]);
  92.     tprintf ("Position reporting to: %s\n", inet_ntoa(PosHost));
  93.     return 0;
  94. }
  95.  
  96. /* Display current position data string */
  97. int
  98. docurrent(argc,argv,p)
  99. int argc OPTIONAL;
  100. char *argv[] OPTIONAL;
  101. void *p OPTIONAL;
  102. {
  103.     tprintf ("Current GPS Position data:\n%s\n", (!CurrentPos || !*CurrentPos) ? "unknown!\n" : CurrentPos);
  104.     return 0;
  105. }
  106.  
  107. static int
  108. doPOSkick(argc,argv,p)
  109. int argc OPTIONAL;
  110. char *argv[] OPTIONAL;
  111. void *p OPTIONAL;
  112. {
  113.     POStick (NULL);
  114.     return 0;
  115. }
  116.  
  117. static int
  118. dolist(argc,argv,p)
  119. int argc OPTIONAL;
  120. char *argv[] OPTIONAL;
  121. void *p OPTIONAL;
  122. {
  123. char buf[GPSMAXLEN + 1], *cp;
  124. FILE *fp;
  125.  
  126.     fp = fopen (GPSfile, "r");    /* no error checking yet */
  127.     if (fp)        {
  128.         tputs ("Current GPS Data:\n\n");
  129. /*        rewind (fp);        */
  130.         while (!feof (fp))    {
  131.             (void) fread (buf, 1, GPSMAXLEN, fp);
  132.             if (feof (fp))
  133.                 continue;
  134. #ifdef nope
  135. #ifdef TNOS_68K
  136.             cp = strpbrk (buf, "\n\l");    /* no error chk */
  137. #else
  138.             cp = strpbrk (buf, "\r\n");    /* no error chk */
  139. #endif
  140.             *(++cp) = 0;
  141. #endif
  142.             cp = strchr (buf, '*'); /* find beginning of checksum */
  143.             if (cp != NULLCHAR)
  144.                 *cp = 0;
  145.             tprintf ("%s\n", buf);
  146.         }
  147.         (void) fclose (fp);
  148.     }
  149.     return 0;
  150. }
  151.  
  152. static int
  153. dotimer(argc,argv,p)
  154. int argc;
  155. char *argv[];
  156. void *p OPTIONAL;
  157. {
  158.     if(argc < 2){
  159.         tprintf("Outgoing Position timer: %lu/%lu\n",
  160.             read_timer(&POStimer)/1000L,
  161.             dur_timer(&POStimer)/1000L);
  162.         return 0;
  163.     }
  164.     stop_timer (&POStimer);    /* just in case */
  165.     POStimer.func = (void (*)(void *))POStick;/* what to call on timeout */
  166.     POStimer.arg = NULL;        /* dummy value */
  167.     set_timer(&POStimer,atol(argv[1])*1000L); /* set timer duration */
  168.     start_detached_timer(&POStimer);     /* fire it up */
  169.     return 0;
  170. }
  171.  
  172. static void
  173. POStick (v)
  174. void *v OPTIONAL;
  175. {
  176. #ifdef nope
  177.     if (GPSactive != NULLIF)        {    /* GPS must be started */
  178.         ksignal (GPSactive, 0);    /* wake up the GPS daemon */
  179.         kwait (CurrentPos);    /* wait until it's update is complete */
  180.     }
  181. #endif
  182.     if (CurrentPos && *CurrentPos)    /* not if nothing there */
  183.         (void) gps_tx();
  184.     start_detached_timer(&POStimer);     /* fire it up */
  185. }
  186.  
  187. /* Process GPS input received from 'interface'. */
  188. static void
  189. gps_rx(iface,sock,cnt)
  190. struct iface *iface OPTIONAL;
  191. struct udp_cb *sock;
  192. int16 cnt OPTIONAL;
  193. {
  194. struct mbuf *bp, **bpp;
  195. struct socket fsock;
  196. char buf[GPSMAXLEN + 1], *cp = buf;
  197. char inbuff[GPSMAXLEN + 1];
  198. FILE *fp;
  199. int hostnamelen;
  200.  
  201.     /* receive the RIP packet */
  202.     (void) recv_udp(sock,&fsock,&bp);
  203.  
  204.     memset (buf, 0x20, GPSMAXLEN);
  205.     while(len_p(bp))    {
  206.         bpp = &bp;
  207.         *cp++ = (char) PULLCHAR(bpp);
  208.     }
  209.     if ((cp = strchr (buf, ':')) == NULLCHAR)    /* not a valid gps frame */
  210.         return;
  211.     hostnamelen = (int) (cp - buf + 1);    /* include the ":" */
  212.     fp = fopen (GPSfile, "a+");    /* no error checking yet */
  213.     if (fp != NULLFILE)    {
  214.         fseek (fp, GPSMAXLEN, 0);    /* don't touch 1st entry */
  215.         while (!feof (fp))    {
  216.             (void) fread (inbuff, 1, GPSMAXLEN, fp);
  217.             if (!strncmp (inbuff, buf, (unsigned)hostnamelen))        { /* found it */
  218.                 fseek (fp, (long) (ftell (fp) - (long) GPSMAXLEN), 0L);
  219.                 break;
  220.             }
  221.         }
  222.         fwrite (buf, 1, GPSMAXLEN, fp);
  223.         (void) fclose (fp);
  224.     }
  225.     return;        
  226. }
  227.  
  228. /* Send a GPS packet to the specified destination */
  229. static int
  230. gps_tx()
  231. {
  232. struct mbuf *bp;
  233. struct socket lsock,fsock;
  234.  
  235.     lsock.address = INADDR_ANY;
  236.     fsock.address = PosHost;
  237.     lsock.port = fsock.port = IPPORT_GPS;
  238.  
  239.     /* Send out one GPS packet as a broadcast to 'dest'  */
  240.     if((bp = alloc_mbuf(GPSMAXLEN)) == NULLBUF)
  241.         return -1;
  242.  
  243.     strncpy ((char *) bp->data, CurrentPos, GPSMAXLEN);
  244.     bp->cnt = (int16) strlen ((char *) bp->data);
  245.     (void) send_udp(&lsock, &fsock,0,0,bp,bp->cnt,0,0);
  246.     return 0;
  247. }
  248.  
  249. #endif
  250.